//
//  AppDelegate.swift
//  Microblog
//
//  Created by steaest on 15/7/28.
//  Copyright © 2015年 steaest. All rights reserved.
//

import UIKit

//在类的外边定义的常量或者变量就是全局能够访问的
//视图控制器切换通知字符串
let WWRootViewControllerSwitchNotification = "WWRootViewControllerSwitchNotification"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchViewController:", name: WWRootViewControllerSwitchNotification, object: nil)
        
        print(UserAccount.sharedAccount)
        
        setColoe()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultViewController()
        
        window?.makeKeyAndVisible()
        
        return true
    }
    
    //MARK:通知注销
    //不写不会有任何影响,因为这句话在程序销毁的时候才执行,习惯而已
    deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //MARK:切换控制器,一定要在AppDelegate中统一修改
    func switchViewController(n: NSNotification) {
    
        let mainVC = n.object as! Bool
        
        window?.rootViewController = mainVC ? MainUITabBarController() : WelcomeViewController()
    }

    
    //默认启动用户是否登录,如果没有登录返回主控制器
    private func defaultViewController() -> UIViewController {
    
        //1.判断用户是否登录,如果没有登录返回主控制器
        if !UserAccount.userLogon {
        
            return MainUITabBarController()
        }
        
        //2.判断是否新版本,如果是,返回新特性,否则返回欢迎
        return isNewUpdate() ? NewCollectionViewController() : WelcomeViewController()
        
    }
    
    //MARK:检查是否有新版本
    private func isNewUpdate() -> Bool {
    
        //1.获取当前的版本
        //print(NSBundle.mainBundle().infoDictionary)
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        
        
        //2.获取 "之前" 的版本,偏好设置
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        
        
        //3.将当前版本保存到偏好设置
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        //iOS7.0直呼不再需要同步
        NSUserDefaults.standardUserDefaults().synchronize()
        
        //4.返回比较结果
        return currentVersion > sandboxVersion
    
    }
    
    //设置外观
    private func setColoe() {
        // 一经设置，全局有效，应该尽早设置
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

